package com.easymin.weixin.controller.res;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxApplicationManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/sales")
public class SalesRes {

  @Autowired
  private WxApplicationManager wxApplicationManager;

  @Autowired
  private DaijiaApi daijiaApi;

  /**
   * 分页查询活动.
   * @param longitude 经度
   * @param latitude 纬度
   * @param acKey 区分公司
   * @param page 页数
   * @param limit 每页数量
   * @param appKey 系统key
   * @return Map
   */
  @RequestMapping(value = {"queryActivity"}, method = RequestMethod.GET)
  public @ResponseBody Map<String, Object> queryActivity(
      @RequestParam("longitude") Double longitude, 
      @RequestParam("latitude") Double latitude,
      @RequestParam(required = false, value = "passengerId") Long passengerId,
      @RequestParam(required = false, value = "acKey") String acKey,
      @RequestParam("page") Integer page,
      @RequestParam("limit") Integer limit,
      @RequestParam("appKey") String appKey) throws Exception {
    appKey = StringUtils.trimToEmpty(appKey);
    Map<String, Object> map = new HashMap<String, Object>();
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    map = daijiaApi.queryActivity(
        wxApplication.getAddress(), longitude, latitude, passengerId, acKey, page, limit);

    return map;
  }
  /**
   * 根据类型查询活动.
   */
  
  @RequestMapping(value = {"queryActivityByService"}, method = RequestMethod.GET)
  public @ResponseBody Map<String, Object> queryActivityByService(
      @RequestParam("longitude") Double longitude,
      @RequestParam("latitude") Double latitude,
      @RequestParam(required = false, value = "passengerId") Long passengerId,
      @RequestParam(required = false, value = "acKey") String acKey,
      @RequestParam("serviceType") String serviceType, 
      @RequestParam("appKey") String appKey)
      throws Exception {
    appKey = StringUtils.trimToEmpty(appKey);

    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    return daijiaApi.queryActivityByService(wxApplication.getAddress(), longitude, latitude, passengerId, acKey, serviceType);
    
  }

}
